<template>
	<view class="page" :style="loading ? 'opacity: 0;' : 'opacity: 1;'">
		<!--  v-model="dataList" @query="queryList" -->
		<pageTxt>
			<z-paging ref="paging" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<!-- 头部 -->
				<template slot="top">
					<view class="navbar-box">
						<u-navbar title="上传设计图" :autoBack="true" :fixed="false"></u-navbar>
					</view>
				</template>
				<view class="add-content ios">
					<u--form labelPosition="top" :model="scsjtRuleForm" ref="uForm" labelWidth="auto">
						<view class="form-box">
							<u-form-item labelPosition="top" labelWidth="100%" borderBottom required label="平面图文件 (.dwg,.pdf,.zip,.pptx,.ppt,.jpg,.png,.gif)">
								<file-upload
									:filesList="scsjtRuleForm.plan_drawingOld"
									:typeList="['m﻿dwg', 'pdf', 'zip', 'pptx', 'ppt', 'jpg', 'png', 'gif']"
									@afterRead="afterRead('plan_drawing', $event)"
									@delete="deletePic('plan_drawing', $event)"
								></file-upload>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" borderBottom required label="效果图文件 (.zip,.ppt,.ppsx,.pptx,.psd,.pdf,.jpg,.png,.gif)">
								<file-upload
									:filesList="scsjtRuleForm.effect_drawingOld"
									:typeList="['zip', 'ppt', 'ppsx', 'pptx', 'psd', 'pdf', 'jpg', 'png', 'gif']"
									@afterRead="afterRead('effect_drawing', $event)"
									@delete="deletePic('effect_drawing', $event)"
								></file-upload>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" borderBottom required label="施工图文件 (.zip,.ppt,.ppsx,.pptx,.psd,.pdf,.dwg,.jpg,.png,.gif)">
								<file-upload
									:filesList="scsjtRuleForm.construction_drawingOld"
									:typeList="['zip', 'ppt', 'ppsx', 'pptx', 'psd', 'pdf', 'dwg', 'jpg', 'png', 'gif']"
									@afterRead="afterRead('construction_drawing', $event)"
									@delete="deletePic('construction_drawing', $event)"
								></file-upload>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" borderBottom label="其他文件">
								<file-upload
									:filesList="scsjtRuleForm.other_fileOld"
									@afterRead="afterRead('other_file', $event)"
									@delete="deletePic('other_file', $event)"
								></file-upload>
							</u-form-item>

							<u-form-item labelPosition="top" labelWidth="100%" label="备注">
								<u--textarea
									disableDefaultPadding
									v-model="scsjtRuleForm.order_demand"
									border="none"
									placeholder="备注"
									:placeholderStyle="{ fontSize: '13.5px', color: '#BFC4CC' }"
								></u--textarea>
							</u-form-item>
						</view>
					</u--form>
				</view>
				<view class="footer ios">
					<u-button
						text="提交"
						@click="submitFunc"
						customStyle="width: 702rpx;height: 88rpx;background: #3B75FF;border-radius: 16rpx;font-weight: 500;font-size: 28rpx;color: #FFFFFF;margin:20rpx 24rpx;"
					></u-button>
				</view>
			</z-paging>
		</pageTxt>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: false,
			list: [],
			scsjtRuleForm: {
				project_id: '',
				remark: '',
				plan_drawing: [],
				effect_drawing: [],
				construction_drawing: [],
				other_file: []
			},
			statusVal: '' //当前项目状态
		};
	},
	onLoad(option) {
		this.scsjtRuleForm.project_id = option.id || '';
		this.designProjectDetails();
	},
	methods: {
		// 设计详情
		designProjectDetails() {
			if (!this.scsjtRuleForm.project_id) return;
			this.$Apipc
				.designProjectDetails({
					project_id: this.scsjtRuleForm.project_id
				})
				.then((res) => {
					console.log(res, '设计图详情');
					if (res.code == 200) {
						let project_file = res.data.project_file;
						this.statusVal = res.data.status;
						this.scsjtRuleForm = {
							delete_arr: [],
							project_id: res.data.id,
							remark: '',
							plan_drawingOld: project_file.plan_drawing,
							effect_drawingOld: project_file.effect_drawing,
							construction_drawingOld: project_file.construction_drawing,
							other_fileOld: project_file.other_file,
							plan_drawing: [],
							effect_drawing: [],
							construction_drawing: [],
							other_file: []
						};
					}
				});
		},
		afterRead(type, files) {
			files.map((item) => {
				let obj = {
					url: item.url
				};
				this.scsjtRuleForm[type].push(obj);
			});
		},
		deletePic(type, e) {
			console.log(e, 'deletePic');
			if (e.file.file_id) {
				e.file.delete = true;
				e.file.type = type;
				// console.log(this.scsjtRuleForm[type + 'Old']);
				// console.log(e.index);
				// this.scsjtRuleForm[type + 'Old'].splice(e.index, 1);
				this.scsjtRuleForm.delete_arr.push(e.file);
				return;
			}
			this.scsjtRuleForm[type].splice(e.index, 1);
		},
		async submitFunc() {
			let scsjtRuleForm = this.scsjtRuleForm;
			console.log(scsjtRuleForm, 'scsjtRuleForm');
			if (!scsjtRuleForm.plan_drawing.length && !scsjtRuleForm.plan_drawingOld.length) {
				uni.showToast({
					title: '请选择平面图文件',
					icon: 'error'
				});
				return;
			}
			if (!scsjtRuleForm.effect_drawing.length && !scsjtRuleForm.effect_drawingOld.length) {
				uni.showToast({
					title: '请选择效果图文件',
					icon: 'error'
				});
				return;
			}
			if (!scsjtRuleForm.construction_drawing.length && !scsjtRuleForm.construction_drawingOld.length) {
				uni.showToast({
					title: '请选择施工图文件',
					icon: 'error'
				});
				return;
			}
			let data = {
				remark: scsjtRuleForm.remark,
				project_id: scsjtRuleForm.project_id,
				delete_arr: scsjtRuleForm.delete_arr
			};
			if (scsjtRuleForm.plan_drawing.length || scsjtRuleForm.effect_drawing.length || scsjtRuleForm.construction_drawing.length || scsjtRuleForm.other_file.length) {
				uni.showLoading({
					title: '文件上传中',
					mask: true
				});
			}
			let plan_drawing = [];
			if (scsjtRuleForm.plan_drawing.length) {
				plan_drawing = await this.$upload(scsjtRuleForm.plan_drawing);
			}
			let effect_drawing = [];
			if (scsjtRuleForm.effect_drawing.length) {
				effect_drawing = await this.$upload(scsjtRuleForm.effect_drawing);
			}
			let construction_drawing = [];
			if (scsjtRuleForm.construction_drawing.length) {
				construction_drawing = await this.$upload(scsjtRuleForm.construction_drawing);
			}
			let other_file = [];
			if (scsjtRuleForm.other_file.length) {
				other_file = await this.$upload(scsjtRuleForm.other_file);
			}
			if (scsjtRuleForm.plan_drawing.length || scsjtRuleForm.effect_drawing.length || scsjtRuleForm.construction_drawing.length || scsjtRuleForm.other_file.length) {
				uni.hideLoading();
			}
			data.plan_drawing = plan_drawing.arrResult || [];
			data.effect_drawing = effect_drawing.arrResult || [];
			data.construction_drawing = construction_drawing.arrResult || [];
			data.other_file = other_file.arrResult || [];
			console.log(data);
			// 添加
			if (this.statusVal == 1015 || this.statusVal == 1060) {
				this.uploadDesignDrawings(data);
			} else {
				// 更新
				if (
					!scsjtRuleForm.plan_drawing.length &&
					!scsjtRuleForm.effect_drawing.length &&
					!scsjtRuleForm.construction_drawing.length &&
					!scsjtRuleForm.other_file.length &&
					!scsjtRuleForm.delete_arr.length
				) {
					uni.showToast({
						title: '没有改动，无需提交1',
						icon: 'error'
					});
					return;
				}
				this.updDesignDrawings(data);
			}
		},
		// 上传设计图
		uploadDesignDrawings(data) {
			uni.showLoading({
				title: '信息提交中'
			});
			this.$Apipc.uploadDesignDrawings(data).then((res) => {
				console.log(res, '上传设计图');
				uni.hideLoading();
				if (res.code == 200) {
					setTimeout(() => {
						var currentPages = getCurrentPages()[getCurrentPages().length - 2]
						if (currentPages) {
							uni.navigateBack({
								delta: 1
							});
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}, 500);
				}
				uni.showToast({
					title: res.code == 200 ? '操作成功' : res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		},
		// 更新设计图
		updDesignDrawings(data) {
			uni.showLoading({
				title: '信息提交中'
			});
			this.$Apipc.updDesignDrawings(data).then((res) => {
				console.log(res, '更新设计图');
				uni.hideLoading();
				if (res.code == 200) {
					setTimeout(() => {
						var currentPages = getCurrentPages()[getCurrentPages().length - 2]
						if (currentPages) {
							uni.navigateBack({
								delta: 1
							});
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}, 500);
				}
				uni.showToast({
					title: res.code == 200 ? '操作成功' : res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
